package drds.connection_pool.proxy;

import drds.connection_pool.ProxyFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public abstract class StatementProxy implements Statement
{
   protected final ConnectionProxy connectionProxy;
   final Statement delegate;

   private boolean isClosed;
   private ResultSet proxyResultSet;

   StatementProxy(ConnectionProxy connectionProxy, Statement statement)
   {
      this.connectionProxy = connectionProxy;
      this.delegate = statement;
   }

   @SuppressWarnings("unused")
   final SQLException checkException(SQLException e)
   {
      return connectionProxy.checkException(e);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public final String toString()
   {
      final String delegateToString = delegate.toString();
      return this.getClass().getSimpleName() + '@' + System.identityHashCode(this) + " wrapping " + delegateToString;
   }

   // **********************************************************************
   //                 Overridden java.sql.Statement Methods
   // **********************************************************************

   /**
    * {@inheritDoc}
    */
   @Override
   public final void close() throws SQLException
   {
      synchronized (this)
      {
         if (isClosed)
         {
            return;
         }

         isClosed = true;
      }

      connectionProxy.untrackStatement(delegate);

      try
      {
         delegate.close();
      } catch (SQLException e)
      {
         throw connectionProxy.checkException(e);
      }
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public Connection getConnection() throws SQLException
   {
      return connectionProxy;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean execute(String sql) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.execute(sql);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean execute(String sql, int autoGeneratedKeys) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.execute(sql, autoGeneratedKeys);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public ResultSet executeQuery(String sql) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      ResultSet resultSet = delegate.executeQuery(sql);
      return ProxyFactory.getResultSetProxy(connectionProxy, this, resultSet);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int executeUpdate(String sql) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeUpdate(sql);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int[] executeBatch() throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeBatch();
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeUpdate(sql, autoGeneratedKeys);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int executeUpdate(String sql, int[] columnIndexes) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeUpdate(sql, columnIndexes);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int executeUpdate(String sql, String[] columnNames) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeUpdate(sql, columnNames);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean execute(String sql, int[] columnIndexes) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.execute(sql, columnIndexes);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean execute(String sql, String[] columnNames) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.execute(sql, columnNames);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public long[] executeLargeBatch() throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeLargeBatch();
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public long executeLargeUpdate(String sql) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeLargeUpdate(sql);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeLargeUpdate(sql, autoGeneratedKeys);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeLargeUpdate(sql, columnIndexes);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public long executeLargeUpdate(String sql, String[] columnNames) throws SQLException
   {
      connectionProxy.markCommitStateDirty();
      return delegate.executeLargeUpdate(sql, columnNames);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public ResultSet getResultSet() throws SQLException
   {
      final ResultSet resultSet = delegate.getResultSet();
      if (resultSet != null)
      {
         if (proxyResultSet == null || ((ResultSetProxy) proxyResultSet).delegate != resultSet)
         {
            proxyResultSet = ProxyFactory.getResultSetProxy(connectionProxy, this, resultSet);
         }
      } else
      {
         proxyResultSet = null;
      }
      return proxyResultSet;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public ResultSet getGeneratedKeys() throws SQLException
   {
      ResultSet resultSet = delegate.getGeneratedKeys();
      if (proxyResultSet == null || ((ResultSetProxy) proxyResultSet).delegate != resultSet)
      {
         proxyResultSet = ProxyFactory.getResultSetProxy(connectionProxy, this, resultSet);
      }
      return proxyResultSet;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   @SuppressWarnings("unchecked")
   public final <T> T unwrap(Class<T> iface) throws SQLException
   {
      if (iface.isInstance(delegate))
      {
         return (T) delegate;
      } else if (delegate != null)
      {
         return delegate.unwrap(iface);
      }

      throw new SQLException("Wrapped statementProxy is not an instance of " + iface);
   }
}
